﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Collections.Generic;
using Cn.Cellula.Model;
using System.Xml;
using Cn.Cellula.Client.ControlHelper;
using System.Text;
using Cn.Edwon.Web;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Product.Category {
    public partial class List : PageSupport,ICellulaBiz {
        ICategoryBiz biz = BizFactory.Get<ICategoryBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
                LoadCaregory(null);
            }
        }

        private void LoadCaregory(String ParentKey) {
            this.lab_CategoryData.Text = "<script type=\"text/javascript\">var CategoryData = " + biz.LoadCategory2Json(this.OwnerKey, this.hid_TypeKey.Value, null) + ";</script>";
        }

        private void InitForm() {
            ClientBiz.BindCodeList(ref ddl_CategoryType, CodeTypeDef.Category.Key, true, null);
            try{
                this.ddl_CategoryType.SelectedValue = Request.QueryString["tid"];
            }catch{}
            this.hid_TypeKey.Value = this.ddl_CategoryType.SelectedValue;
        }

        protected void btn_type_add_Click(object sender, EventArgs e) {
            String parms = Request.Form["cbx_category"];
            if (!String.IsNullOrEmpty(parms)) {
                String[] keys = parms.Split(',');
                Response.Redirect("Edit.aspx?tid=" + this.hid_TypeKey.Value + "&pid=" + keys[0]);
            } else {
                Response.Redirect("Edit.aspx?tid=" + this.hid_TypeKey.Value);
            }
        }

        protected void btn_type_update_Click(object sender, EventArgs e) {
            String parms = Request.Form["cbx_category"];
            if (!String.IsNullOrEmpty(parms)) {
                String[] keys = parms.Split(',');
                Response.Redirect("Edit.aspx?tid=" + this.hid_TypeKey.Value + "&id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
            }
        }

        protected void btn_type_delete_Click(object sender, EventArgs e) {
            String parms = Request.Form["cbx_category"];
            if (!String.IsNullOrEmpty(parms)) {
                String[] keys = parms.Split(',');
                try {
                    biz.DeleteCategories(keys,this.OwnerKey);
                    BizFactory.Get<ILogBiz>().LogDelete("产品分类", keys.Length, this.Actor, Request);
                } catch (Exception ex) {
                    SiteInfo.Send(Page, TxtDef.ERROR, ex.Message);
                }
                this.LoadCaregory(null);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
            }
        }

        protected void ddl_type_SelectedIndexChanged(object sender, EventArgs e) {
            this.LoadCaregory(null);
        }


        #region ICellulaBiz 成员

        public string ModelName {
            get { return CodeTypeDef.Module.Category; }
        }

        #endregion

        protected void ddl_CategoryType_SelectedIndexChanged(object sender, EventArgs e) {
            Response.Redirect("List.aspx?tid=" + this.ddl_CategoryType.SelectedValue);
        }
    }
}
